﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nantianmen.Shared
{
    public class NantianmenResult
    {
        private static readonly NantianmenResult _success = new NantianmenResult { Succeeded = true };
        private readonly List<NantianmenError> _errors = new List<NantianmenError>();

        public bool Succeeded { get; protected set; }

        public IEnumerable<NantianmenError> Errors => _errors;

        public static NantianmenResult Success => _success;

        public static NantianmenResult Failed(params NantianmenError[] errors)
        {
            return BuildFailedResult(errors);
        }

        public static NantianmenResult Failed(List<NantianmenError> errors)
        {
            return BuildFailedResult(errors);
        }

        private static NantianmenResult BuildFailedResult(IEnumerable<NantianmenError> errors)
        {
            var result = new NantianmenResult { Succeeded = false };
            if (errors != null)
            {
                result._errors.AddRange(errors);
            }
            return result;
        }

        public override string ToString()
        {
            return Succeeded ?
                "Succeeded" :
                string.Format(CultureInfo.InvariantCulture, "{0} : {1}", "Failed", string.Join(",", Errors.Select(x => x.Code)));
        }
    }
}
